﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Xml.Linq;
using com.codeplex.eatoutadvocate.Web.Mvc.Models;

namespace com.codeplex.eatoutadvocate.Web.Mvc.Services
{
    public class WeatherService
    {
        # region properties
        private string currentWeather;

        public string CurrentWeather
        {
            get { return currentWeather; }
            set { currentWeather = value; }
        }

        private float currentTemp;

        public float CurrentTemp
        {
            get { return currentTemp; }
            set { currentTemp = value; }
        }
        public string AppKey;
        #endregion


        public WeatherService() { }

        public void Set(string location)
        {
            string url = String.Format(
                "http://api.wunderground.com/api/{0}/conditions/q/{1}.xml",
                AppKey, location);
            XDocument xdoc = SendRequestGetXml(url);

            XElement currentObservation = xdoc.Root.Element("current_observation");
            CurrentWeather = currentObservation.Element("weather").Value;
            float temp_f;
            float.TryParse(currentObservation.Element("temp_f").Value, out temp_f);
            CurrentTemp = temp_f;
        }

        public Weather GetWeather()
        {
            //string currentWeather = weather.CurrentWeather.Trim();
            switch (currentWeather)
            {
                case "Overcast":
                case "Clear":
                case "Partly Cloudy":
                case "Mostly Cloudy":
                case "Scattered Clouds":
                    return Models.Weather.Pleasant;
                default:
                    return Models.Weather.Adverse;
            }
        }

        /// <summary>
        /// Takes the weather object generated after calling get method and 
        /// uses the CurrentTemp property to generate a Temperature enum.
        /// </summary>
        /// <param name="weather">Weather object after calling get method</param>
        /// <returns>Temerature enum based on temperature of Weather object</returns>
        public Temperature GetTemperature()
        {
            if (currentTemp < 40)
            {
                return Models.Temperature.Cold;
            }
            else if (currentTemp >= 40 && currentTemp < 65)
            {
                return Models.Temperature.Mild;
            }
            else if (currentTemp >= 65 && currentTemp < 85)
            {
                return Models.Temperature.Warm;
            }
            else
            {
                return Models.Temperature.Hot;
            }
        }

        private XDocument SendRequestGetXml(string url)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Stream output = response.GetResponseStream();
            XDocument xdoc = XDocument.Load(output);
            return xdoc;
        }

        // Will automatically populate Weather and Temperatures by calling webservice
        public void Populate(string location, string appkey)
        {
            AppKey = appkey;
            Set(location);
        }
    }
}
